package cn.sunjiachao.blog4jcs.modules.blog.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.sunjiachao.blog4jcs.modules.blog.dao.BlogDao;
import cn.sunjiachao.blog4jcs_common.model.Blog;
import cn.sunjiachao.blog4jcs_common.util.web.Page;

/**
 * User: Seven Date: 12-9-23 Time: 下午4:22
 */
@Repository
public class BlogDaoImp implements BlogDao {

	private JdbcTemplate jdbcTemplate;

	@Resource
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Override
	public List<Blog> listAll() {
		String sql = "select blogId,blogTitle,blogContent,createTime,createdUser,blogType,blogText from Blog";
		List<Blog> blogs = jdbcTemplate.query(sql, new RowMapper<Blog>() {
			@Override
			public Blog mapRow(ResultSet rs, int rowNum) throws SQLException {
				Blog blog = new Blog();
				blog.setBlogId(rs.getLong("blogId"));
				blog.setBlogTitle(rs.getString("blogTitle"));
				blog.setBlogContent(rs.getString("blogContent"));
				blog.setCreateTime(rs.getDate("createTime"));
				blog.setCreatedUser(rs.getLong("createdUser"));
				blog.setBlogType(rs.getInt("blogType"));
				blog.setBlogText(rs.getString("blogText"));
				return blog;
			}
		});
		return blogs;
	}

	@Override
	public void save(Blog blog) {
		Object[] params = new Object[] { blog.getBlogTitle(), blog.getBlogContent(), blog.getCreateTime(),
				blog.getCreatedUser(), blog.getBlogType(), blog.getBlogText() };
		String sql = "insert into Blog(blogTitle,BlogContent,createTime,createdUser,blogType,blogText) values(?,?,?,?,?,?)";
		jdbcTemplate.update(sql, params);
	}

	@Override
	public Page<Blog> queryForPage(int currentPage, int numsPerPage) {
		currentPage = (currentPage < 1) ? 1 : currentPage;
		numsPerPage = (numsPerPage < 1) ? 1 : numsPerPage;
		int startIndex = (currentPage - 1) * numsPerPage;
		long count = jdbcTemplate.queryForLong("select count(*) from Blog");
		String pageSql = "select * from blog limit ?,?";
		List<Blog> list = jdbcTemplate.query(pageSql, BeanPropertyRowMapper.newInstance(Blog.class), new Object[] {
				startIndex, numsPerPage });

		return new Page<Blog>(list, count, currentPage, numsPerPage);
	}

	@Override
	public Blog getById(Long id) {
		String sql = "select blogId,blogTitle,blogContent,createTime,createdUser,blogType,blogText from blog where blogId=?";
		Blog blog = jdbcTemplate.queryForObject(sql, new RowMapper<Blog>() {

			@Override
			public Blog mapRow(ResultSet rs, int rowNum) throws SQLException {
				Blog blog = new Blog();
				blog.setBlogId(rs.getLong("blogId"));
				blog.setBlogTitle(rs.getString("blogTitle"));
				blog.setBlogContent(rs.getString("blogContent"));
				blog.setCreateTime(rs.getDate("createTime"));
				blog.setCreatedUser(rs.getLong("createdUser"));
				blog.setBlogType(rs.getInt("blogType"));
				blog.setBlogText(rs.getString("blogText"));
				return blog;
			}
		}, new Object[] { id });
		return blog;
	}

}
